﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLTV
{
    public partial class frmPhieuTraSach : Form
    {
        public frmPhieuTraSach()
        {
            InitializeComponent();
        }

        private void frmPhieuTraSach_Load(object sender, EventArgs e)
        {
            int maPhieuTra = PhieuTraService.LayMaPhieuTraMax();
            txtMaPhieuTra.Text = Convert.ToString(maPhieuTra+1);
            List<TheLoaiDTO> list = TheLoaiService.LayDanhSachTheLoai();
            
            txtTienPhatKyNay.Text = "0";
            txtTongNo.Text = "0";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaDG_TextChanged(object sender, EventArgs e)
        {
            DataTable BangSach = new DataTable();
            try
            {
                int  maDG = int.Parse(txtMaDG.Text);
                DocGiaDTO docGiaDTO = DocGiaService.LayThongTinDocGia(maDG);
                txtHoTen.Text = docGiaDTO.HoTen;
                txtDiaChi.Text = docGiaDTO.DiaChi;
                txtTienNo.Text = docGiaDTO.TienNo.ToString();
                int ngay = docGiaDTO.NgaySinh.Day;
                int thang = docGiaDTO.NgaySinh.Month;
                int nam = docGiaDTO.NgaySinh.Year;
                string NgaySinh = ngay + "/" +thang +"/" + nam;
                txtNgaySinh.Text = NgaySinh;               

                BangSach =PhieuTraService.LayThongTinSachTra(maDG);
                dgvDSSachTra.DataSource = BangSach;                
                  
            }
            catch (System.Exception)
            { 
            }
        }

        private void btnLapPhieu_Click(object sender, EventArgs e)
        {
            PhieuTraDTO phieuTra = new PhieuTraDTO();
            CT_PhieuTraDTO chiTietPT = new CT_PhieuTraDTO();
            DocGiaDTO docGiaDTO = new DocGiaDTO();
            int soSachTra = 0;
            float tienPhat = 0;            
            //Lap phieu tra sach
            phieuTra.MaPhieuTra = int.Parse(txtMaPhieuTra.Text);
            phieuTra.NgayTra = Convert.ToDateTime(dtpNgayTra.Text);
            phieuTra.MaDocGia = int.Parse(txtMaDG.Text);

            PhieuTraService.LapPhieuTraSach(phieuTra);
            docGiaDTO = DocGiaService.LayThongTinDocGia(phieuTra.MaDocGia);

            chiTietPT.MaPhieuTra = phieuTra.MaPhieuTra;
            int soDong = dgvDSSachTra.RowCount;
            for (int i = 0; i < soDong-1; i++)
            {                
                if ( dgvDSSachTra.Rows[i].Cells["DaTra"].Value!=null)           
                {
                    bool test = bool.Parse(dgvDSSachTra.Rows[i].Cells["DaTra"].Value.ToString());
                    if (test == true)
                    {
                        soSachTra += 1;
                        tienPhat +=Convert.ToInt32( dgvDSSachTra.Rows[i].Cells[8].Value);
                        int maSach = Convert.ToInt32(dgvDSSachTra.Rows[i].Cells["MaSach"].Value);
                        SachDTO sachDTO = SachService.LayThongTinSach(maSach);
                        sachDTO.TinhTrangSach = 1;
                        chiTietPT.MaCT_PhieuMuon = Convert.ToInt32(dgvDSSachTra.Rows[i].Cells[2].Value);
                        chiTietPT.TienPhat = Convert.ToInt32(dgvDSSachTra.Rows[i].Cells[8].Value);
                        CT_PhieuTraService.ThemCTPhieuTra(chiTietPT);
                        SachService.CapNhatSach(sachDTO);
                    }
                }
            }
            //Cap nhat lai Tien no va So sach dang muon cua Doc gia
            docGiaDTO.SoSachDangMuon -= soSachTra;
            docGiaDTO.TienNo += (float)tienPhat;
            DocGiaService.CapNhatDocGia(docGiaDTO);
        }

        private void dgvDSSachTra_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            float tienPhat = 0;
            float tienNo = 0;
            int soDong = dgvDSSachTra.RowCount;
            int maDG = int.Parse(txtMaDG.Text);
            DocGiaDTO docGiaDTO = DocGiaService.LayThongTinDocGia(maDG);
            for (int i = 0; i < soDong - 1; i++)
            {
                if (dgvDSSachTra.Rows[i].Cells["DaTra"].Value != null)
                {
                    bool test = bool.Parse(dgvDSSachTra.Rows[i].Cells["DaTra"].Value.ToString());
                    if (test == true)
                    {
                        tienPhat += Convert.ToInt32(dgvDSSachTra.Rows[i].Cells[8].Value);
                        tienNo = docGiaDTO.TienNo + tienPhat;
                        txtTienPhatKyNay.Text = tienPhat.ToString();
                        txtTongNo.Text = tienNo.ToString();                        
                        
                    }
                }
            }                        
            
        }
    }
}
